/*
 * @Author: bianwangyang bianwy025@qq.com
 * @Date: 2023-11-09 20:32:12
 * @LastEditors: bianwangyang bianwy025@qq.com
 * @LastEditTime: 2023-11-11 19:07:07
 */

/**
 * 移除对象中自动创建的时间字段
 * @param obj
 * @returns
 */
const removeTime = (obj) => {
  if (Array.isArray(obj)) {
    obj = obj.map((_item) => {
      return removeTime(_item);
    });
    return obj;
  } else {
    const { createdAt, publishedAt, updatedAt, ...params } = obj || {};
    Object.getOwnPropertyNames(params).forEach((item) => {
      if (typeof params[item] === "object") {
        if (Array.isArray(params[item])) {
          params[item] = params[item].map((_item) => {
            return removeTime(_item);
          });
        } else {
          params[item] = removeTime(params[item]);
        }
      }
    });
    return params;
  }
};

/**
 * 移除属性和id
 * @param {*} obj
 * @returns
 */

const removeAttrsAndId = (obj) => {
  if (Array.isArray(obj)) {
    obj = obj.map((_item) => {
      return removeAttrsAndId(_item);
    });
    return obj;
  } else {
    const { attributes, id, ...params } = obj || {};
    const newObj = { ...attributes, ...params };

    Object.getOwnPropertyNames(newObj).forEach((item) => {
      if (typeof newObj[item] === "object") {
        if (Array.isArray(newObj[item])) {
          newObj[item] = newObj[item].map((ele) => {
            return removeAttrsAndId(ele);
          });
        } else {
          newObj[item] = removeAttrsAndId(newObj[item]);
        }
      }
    });
    return newObj;
  }
};

module.exports = { removeTime, removeAttrsAndId };
